<!DOCTYPE HTML>
<html>
<head>
  <title>Notification Basics</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="MockServices.js"></script>
  <script type="text/javascript" src="NotificationTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
<script type="text/javascript">

  var info = NotificationTest.info;
  var options;

  SimpleTest.requestFlakyTimeout("untriaged");

  var steps = [
    function () {
      info("Test notification spec");
      ok(Notification, "Notification constructor exists");
      ok(Notification.permission, "Notification.permission exists");
      ok(Notification.requestPermission, "Notification.requestPermission exists");
      ok(Notification.get, "Notification.get exists");
    },

    function () {
      info("Test requestPermission without callback");
      Notification.requestPermission();
    },

    function (done) {
      info("Test requestPermission deny");
      function assertPermissionDenied(perm) {
        is(perm, "denied", "Permission should be denied.");
        is(Notification.permission, "denied", "Permission should be denied.");
      }
      NotificationTest.denyNotifications();
      Notification.requestPermission()
        .then(assertPermissionDenied)
        .then(_ => Notification.requestPermission(assertPermissionDenied))
        .catch(err => {
          ok(!err, "requestPermission should not reject promise");
        })
        .then(done);
    },

    function (done) {
      info("Test requestPermission grant");
      function assertPermissionGranted(perm) {
        is(perm, "granted", "Permission should be granted.");
        is(Notification.permission, "granted", "Permission should be granted");
      }
      NotificationTest.allowNotifications();
      Notification.requestPermission()
        .then(assertPermissionGranted)
        .then(_ => Notification.requestPermission(assertPermissionGranted))
        .catch(err => {
          ok(!err, "requestPermission should not reject promise");
        })
        .then(done);
    },

    function (done) {
      info("Test invalid requestPermission");
      Notification.requestPermission({})
        .then(_ => {
          ok(false, "Non callable arg to requestPermission should reject promise");
        }, err => {
          ok(true, "Non callable arg to requestPermission should reject promise");
        })
        .then(done);
    },

    function (done) {
      info("Test create notification");

      options = NotificationTest.payload;

      var notification = new Notification("This is a title", options);

      ok(notification, "Notification exists");
      is(notification.onclick, null, "onclick() should be null");
      is(notification.onshow, null, "onshow() should be null");
      is(notification.onerror, null, "onerror() should be null");
      is(notification.onclose, null, "onclose() should be null");
      is(typeof notification.close, "function", "close() should exist");
      is(typeof notification.data, "object", "data should be a JS object");

      is(notification.dir, options.dir, "auto should get set");
      is(notification.lang, options.lang, "lang should get set");
      is(notification.body, options.body, "body should get set");
      is(notification.tag, options.tag, "tag should get set");
      is(notification.icon, options.icon, "icon should get set");
      ok(NotificationTest.customDataMatches(notification.data),
         "data should get set");

      // store notification in test context
      this.notification = notification;

      notification.onshow = function () {
        ok(true, "onshow handler should be called");
        done();
      };
    },

    function (done) {
      info("Test closing a notification");
      var notification = this.notification;

      notification.onclose = function () {
        ok(true, "onclose handler should be called");
        done();
      };

      notification.close();
    }
  ];

  MockServices.register();
  NotificationTest.run(steps, function () {
    MockServices.unregister();
  });
</script>
</body>
</html>
